
package panels;

import core.COINCOMOComponent;
import core.COINCOMOSubSystem;
import core.COINCOMOSystem;
import core.COINCOMOUnit;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import main.GlobalMethods;

/**
 *
 * @author Raed Shomali
 */
public class OverviewsAndGraphsPanel extends JPanel implements ActionListener, ChangeListener
{
    public static JTabbedPane tabs = new JTabbedPane() ;
    
    public static COPSEMOPanel copsemo = new COPSEMOPanel() ;
    
    public OverviewsAndGraphsPanel()
    {
        tabs.setFocusable( false );
        
        tabs.addTab( "Overview" , new JPanel() );
        tabs.addTab( "COPSEMO" , new JPanel() );
        tabs.addTab( "COSYSMO" , new JPanel() );
        tabs.addTab( "COCOTS", new JPanel() );
        //tabs.addTab( "Extension 1" , new JPanel() );
        //tabs.addTab( "Extension 2" , new JPanel() );
        
        tabs.setEnabledAt( 1 , false );
        tabs.setEnabledAt( 2 , false );
        tabs.setEnabledAt( 3 , false);

        tabs.addChangeListener( this );
        this.setLayout( new BorderLayout() );
        this.add( tabs ) ;
    }
    
    public static void clearOverviewTab()
    {
        tabs.setSelectedIndex( 0 );
        tabs.setTitleAt( 0 , "Overview" ) ;
        tabs.setComponentAt( 0 , new JPanel() );
    }
    
    /**
     * 
     * @param selectedUnit is used to update the Tab with respective unit content
     */
    public static void updateOverviewTabWith( COINCOMOUnit selectedUnit )
    {
        tabs.setEnabledAt( 1 , false );
        tabs.setComponentAt( 1 ,  new JPanel() );
        tabs.setEnabledAt( 2 , false );
        tabs.setComponentAt( 2 ,  new JPanel() );
        tabs.setEnabledAt( 3 , false );
        tabs.setComponentAt( 3 ,  new JPanel() );
        
        // Determine Which Panel To Use
        if( selectedUnit instanceof COINCOMOSystem )
        {
            tabs.setComponentAt( 0 , new SystemOverviewPanel( (COINCOMOSystem) selectedUnit ) );
            tabs.setEnabledAt( 2 , true );
            tabs.setComponentAt( 2 , new CosysmoverviewPanel((COINCOMOSystem) selectedUnit));
            
           // tabs.setComponentAt( 2 , cosysmo );
        }
        else if( selectedUnit instanceof COINCOMOSubSystem )
        {
            tabs.setComponentAt( 0 , new SubSystemOverviewPanel( (COINCOMOSubSystem) selectedUnit ) );
            tabs.setEnabledAt( 2 , true );
            tabs.setComponentAt( 2 , new CosysmoverviewPanelSS((COINCOMOSubSystem) selectedUnit));
          //  tabs.setComponentAt( 2 , new CosysmoverviewPanel());
          //  tabs.setComponentAt( 2 , cosysmo );
        }
        else  if  (selectedUnit instanceof COINCOMOComponent)// COINCOMO Component
        {
            tabs.setComponentAt( 0 , new ComponentOverviewPanel( (COINCOMOComponent) selectedUnit ) );
            
            tabs.setEnabledAt( 1 , true );
            tabs.setEnabledAt( 2 , false );
            tabs.setEnabledAt( 3 , true );

            tabs.setEnabledAt( 3 , true );
            tabs.setComponentAt( 3 , new COCOTSComponentPanel((COINCOMOComponent) selectedUnit));
            
            tabs.setComponentAt( 1 , copsemo );
            
            // Show the Loading Text ...
            COPSEMOPanel.graph.isLoading = true ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "Loading COPSEMO ..." );
            
            OverviewsAndGraphsPanel.copsemo.updateCOPSEMO( (COINCOMOComponent) selectedUnit );
            
            // Show the Graph ..
            COPSEMOPanel.graph.isLoading = false ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "COPSEMO Loaded." );

            
        }
        
       else {
            
            //tabs.setComponentAt( 0 , new CosysmoverviewPanel());
            
            tabs.setEnabledAt( 1 , true );
            tabs.setComponentAt( 1 , copsemo );
            
            // Show the Loading Text ...
            COPSEMOPanel.graph.isLoading = true ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "Loading COPSEMO ..." );
            
            OverviewsAndGraphsPanel.copsemo.updateCOPSEMO( (COINCOMOComponent) selectedUnit );
            
            // Show the Graph ..
            COPSEMOPanel.graph.isLoading = false ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "COPSEMO Loaded." );
        
            
            
            
            
 }


        
        // Change Overview's Tab's Title
        tabs.setTitleAt( 0 , selectedUnit.getName() + "'s Overview" );
        
        // If COPSEMO Is Disabled ...
        if( !tabs.isEnabledAt( 1 ) )
        {
            // Show the Overview Panel
            tabs.setSelectedIndex( 0 );
        }
    }

    /**
     *
     * @param selectedUnit is used to update the Tab with respective unit content
     */
    public static void updateCosysmoTabWith( COINCOMOUnit selectedUnit )
    {
        tabs.setEnabledAt( 1 , false );
        tabs.setComponentAt( 1 ,  new JPanel() );
        tabs.setEnabledAt( 2 , true );
        tabs.setComponentAt( 2 ,  new JPanel() );

        // Determine Which Panel To Use
        if( selectedUnit instanceof COINCOMOSystem )
        {
            tabs.setComponentAt( 0 , new SystemOverviewPanel( (COINCOMOSystem) selectedUnit ) );
             tabs.setEnabledAt( 2 , true );
        }
        else if( selectedUnit instanceof COINCOMOSubSystem )
        {
            tabs.setComponentAt( 0 , new SubSystemOverviewPanel( (COINCOMOSubSystem) selectedUnit ) );
            tabs.setEnabledAt( 2 , true );
        }
        else    // COINCOMO Component
        {
            tabs.setComponentAt( 0 , new ComponentOverviewPanel( (COINCOMOComponent) selectedUnit ) );

            tabs.setEnabledAt( 1 , true );
            tabs.setEnabledAt( 2 , false );
            tabs.setComponentAt( 1 , copsemo );

            // Show the Loading Text ...
            COPSEMOPanel.graph.isLoading = true ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "Loading COPSEMO ..." );

            OverviewsAndGraphsPanel.copsemo.updateCOPSEMO( (COINCOMOComponent) selectedUnit );

            // Show the Graph ..
            COPSEMOPanel.graph.isLoading = false ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "COPSEMO Loaded." );
        }

        // Change Overview's Tab's Title
        tabs.setTitleAt( 0 , selectedUnit.getName() + "'s Overview" );

        // If COPSEMO Is Disabled ...
        if( !tabs.isEnabledAt( 1 ) )
        {
            // Show the Overview Panel
            tabs.setSelectedIndex( 0 );
        }
    }
    
    public void stateChanged( ChangeEvent e )
    {
        JTabbedPane pane = (JTabbedPane)e.getSource();

        //component.setSizingType( pane.getSelectedIndex() );
        
        //pane.setBackgroundAt(pane.getSelectedIndex(), Color.green);
        if(tabs.getTitleAt(pane.getSelectedIndex()).equals("COCOTS"))
        	((COCOTSComponentPanel)tabs.getComponentAt(pane.getSelectedIndex())).loadcomboBoxSubComponents();
        //((COCOTSComponentPanel)pane).loadcomboBoxSubComponents();
    }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
